package com.amountconvert.common;

public class MathEx {
	public static double roundDouble(double val, int precision) {
		double ret = 0;
		try {
			double factor = pow(10, precision);
			ret = Math.floor(val * factor + 0.5) / factor;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ret;
	}

	public static double pow(double x, double y) {
		int den = 1024;
		int num = (int) (y * den);
		int iterations = 10;
		double n = Double.MAX_VALUE;
		while (n >= Double.MAX_VALUE && iterations > 1) {
			n = x;
			for (int i = 1; i < num; i++)
				n *= x;
			if (n >= Double.MAX_VALUE) {
				iterations--;
				den = (int) (den / 2);
				num = (int) (y * den);
			}
		}
		for (int i = 0; i < iterations; i++) {
			n = Math.sqrt(n);
		}
		return n;
	}
}
